package com.sczmgk.hotline_pzh.ui;

import android.os.Bundle;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.base.BaseActivity;
import com.sczmgk.hotline_pzh.callback.VerfyCallback;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.dialog.DialogFactory;
import com.sczmgk.hotline_pzh.dialog.MessageDialog;
import com.sczmgk.hotline_pzh.entity.BaseEntity;
import com.sczmgk.hotline_pzh.entity.IndicatorInfos;
import com.sczmgk.hotline_pzh.utils.CommonUtils;
import com.sczmgk.hotline_pzh.utils.MyToast;
import com.sczmgk.hotline_pzh.utils.SecurityCodeUtils;
import com.sczmgk.hotline_pzh.utils.TimerUtils;
import com.sczmgk.hotline_pzh.utils.Tools;
import com.sczmgk.hotline_pzh.utils.UIUtils;
import com.sczmgk.hotline_pzh.view.RegistItemView;

import java.util.List;

/**
 * Created by Administrator on 2016/12/12 0012.
 */
public class RegistActivity extends BaseActivity implements View.OnClickListener {

    private Button mVerifyBtn;
    /*下一步的点击事件*/
    private Button mBtnNext;

    /*手机号,验证码的控件*/
    private RegistItemView mRegistPhone;
    private RegistItemView mRegistKey;

    /*密码的控件,当前界面需隐藏*/
    private RegistItemView mRegistPwd;

    /*注册协议的条目控件,当前界面需隐藏*/
    private LinearLayout mRegistShowProtocol;
    private MessageDialog errorDialog;
    private TimerUtils timerUtils;

    @Override
    public void handleMessage(Message msg) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);

        //添加activity到集合中
        CommonUtils.addActivity(this);

        setHeader();

        initView();

        initData();

        setListener();
    }

    /**
     * 设置标题栏
     */
    private void setHeader() {
        setTitle(R.string.regist);
        setLeftBtnVisibility(View.VISIBLE);
    }

    /**
     * 初始化控件
     */
    private void initView() {

        //手机号
        mRegistPhone = (RegistItemView) findViewById(R.id.riv_regist_phone);
        //获取验证码按钮
        mVerifyBtn = mRegistPhone.getVerifyButton();

        //验证码
        mRegistKey = (RegistItemView) findViewById(R.id.riv_regist_security_code);
        //密码
        mRegistPwd = (RegistItemView) findViewById(R.id.riv_regist_pwd);

        //协议条目
        mRegistShowProtocol = (LinearLayout) findViewById(R.id.ll_show_protocol);

        //设置下方线条的显示
        View viewLine = findViewById(R.id.view_line) ;
        viewLine.setVisibility(View.GONE);
        View viewLineNext = findViewById(R.id.view_line_next) ;
        viewLineNext.setVisibility(View.GONE);

        //将协议和密码条目设为不显示
        mRegistShowProtocol.setVisibility(View.GONE);
        mRegistPwd.setVisibility(View.GONE);

        //设置手机号和验证码的描述图片
        mRegistPhone.setImageResource(R.drawable.phone);
        mRegistKey.setImageResource(R.drawable.key);

        //下一步
        mBtnNext = (Button) findViewById(R.id.btn_regist_next);

        //设置输入类型
        mRegistPhone.setInputType(InputType.TYPE_CLASS_PHONE);
        mRegistKey.setInputType(InputType.TYPE_CLASS_PHONE);

    }

    /**
     * 初始化注册界面的数据
     */
    private void initData() {

    }

    /**
     * 设置注册界面的监听
     */
    private void setListener() {
        //获取验证码
        mVerifyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               final String phone = mRegistPhone.getText();
                //获取验证码
                boolean isPhone = Tools.isMobilePhoneNumber(phone);
                if (!isPhone) {
                    MyToast.show(RegistActivity.this, "请输入正确的手机号");
                    return;
                }else{
                    SecurityCodeUtils.getCode(RegistActivity.this,phone,1, 0 ,new VerfyCallback() {
                        @Override
                        public void onSuccess() {
                            timerUtils = new TimerUtils();
                            timerUtils.startTimer(RegistActivity.this , mVerifyBtn);
                            MyToast.show(RegistActivity.this,"验证码已发送至:"+phone);
                        }

                        @Override
                        public void onError(BaseEntity.MyError mError) {
                            if (errorDialog == null) {
                                errorDialog = DialogFactory.warningDialog(RegistActivity.this, mError.msg);
                            }else if (!errorDialog.isShowing()){
                                errorDialog = DialogFactory.warningDialog(RegistActivity.this, mError.msg);
                            }
                        }

                        @Override
                        public void onLoadIndicatorDataCallback(List<IndicatorInfos> indicatorInfos) {

                        }
                    });
                }

            }
        });

        //下一步按钮的点击事件
        mBtnNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.btn_regist_next:
                //下一步按钮的点击事件

                String phoneInput = mRegistPhone.getText();
                String keyInput = mRegistKey.getText();

                if (Tools.isEmpty(phoneInput)) {
                    MyToast.show(RegistActivity.this, "手机号不能为空");
                    return;
                }

                if (Tools.isEmpty(keyInput)) {
                    MyToast.show(RegistActivity.this, "请输入验证码!");
                    return;
                }

                //判断是否为手机号
                boolean isPhone = Tools.isMobilePhoneNumber(phoneInput);

                if (isPhone) {
                    //提交数据
                    submitData(phoneInput, keyInput);
                } else {
                    MyToast.show(RegistActivity.this, "请输入正确的电话号码!");
                }

                break;

        }
    }

    /**
     * 提交数据
     *
     * @param phoneInput 输入的手机号
     * @param keyInput   输入的验证码
     */
    private void submitData(final String phoneInput, final String keyInput) {

        //TODO 需要先进行验证码的校验后再跳转,暂时先直接跳转
        SecurityCodeUtils.verfiCode(RegistActivity.this, phoneInput, keyInput, new VerfyCallback() {
            @Override
            public void onSuccess() {

                UIUtils.startActivity(RegistActivity.this, RegistConfirmActivity.class, Constants.REGIST_PHONE_NUMBER, phoneInput, Constants.REGIST_KEY, keyInput);
            }

            @Override
            public void onError(BaseEntity.MyError mError) {
              DialogFactory.warningDialog(RegistActivity.this,mError.msg);
            }

            @Override
            public void onLoadIndicatorDataCallback(List<IndicatorInfos> indicatorInfos) {

            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (timerUtils != null && timerUtils.timerIsRunning()){
            timerUtils.stopTimer(mVerifyBtn);
        }
    }

    @Override
    public boolean getIsNeedLogin() {
        return false;
    }

}
